package com.hgsoft.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hgsoft.dao.TimeTableDao;
import com.hgsoft.entity.TimeTable;
import com.hgsoft.util.Order;
import com.hgsoft.util.Pager;
import com.hgsoft.util.Property;

@Service
public class TimeTableService extends BaseService<TimeTable> {

    @Resource(type=TimeTableDao.class)
    public void setDao(TimeTableDao dao) {
	super.setDao(dao);
    }

    @Override
	public TimeTableDao getDao() {
	return (TimeTableDao) super.getDao();
    }

    public void saveList(List<TimeTable> list) {
	for (TimeTable temp : list) {
	    this.save(temp);

	}
    }
    
    public void delete(Integer line ,Date date) {
	    this.getDao().delete(line, date);
    }

    public boolean findByTimeTableId(String timeTableId) {
	List<TimeTable> list = getDao().findAll(Order.desc("id"), Property.eq("timeTableId", timeTableId));
	if (list != null && !list.isEmpty()) {
	    return true;
	} else {
	    return false;
	}
    }

    public List<TimeTable> query(Pager pager, TimeTable query, String startTime, String endTime) {

	List<Property> properties = new ArrayList<Property>();
	
	if (query != null) {
	    if (query.getTimeTableId() != null && !"".equals(query.getTimeTableId())) {
		properties.add(Property.eq("timeTableId", query.getTimeTableId()));
	    }
	}
	
	if(startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)) {
	    properties.add(Property.between("arrive", startTime, endTime));
	    properties.add(Property.between("depart", startTime, endTime));
	}

	return getDao().findByPager(pager, new Order[] { Order.desc("id") }, properties.toArray(new Property[properties.size()]));
    }

    @SuppressWarnings("rawtypes")
    public List count1(String timeTableID, String startTime, String endTime) {
	return getDao().count1(timeTableID, startTime, endTime);
    }
    
    @SuppressWarnings("rawtypes")
    public List count3(String timeTableID, String startTime, String endTime, String startStation, String endStation, Integer side) {
	return getDao().count3(timeTableID, startTime, endTime, startStation, endStation, side);
    }
    
    @SuppressWarnings("rawtypes")
    public List count2() {
	return getDao().count2();
    }
}
